package org.crazyit.activiti_d1._7_AddBpmnModel;

import org.activiti.bpmn.model.*;
import org.activiti.bpmn.model.Process;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.DeploymentBuilder;

import java.io.FileNotFoundException;

public class AddBpmnModel {

    public static void main(String[] args) throws FileNotFoundException {

        //1. 创建流程引擎
        ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();

        //2. 得到流程存储服务对象
        RepositoryService repositoryService = engine.getRepositoryService();

        //3. 创建DeploymentBuilder 实例
        DeploymentBuilder builder = repositoryService.createDeployment();

        //4. 添加BPMN 模型资源
        builder.addBpmnModel("MyCodeProcess",createProcessModel()).name("MyCodeDeploy").deploy();

    }

    public static BpmnModel createProcessModel() {

        //1. 创建BPMN 模型对象
        BpmnModel model = new BpmnModel();
        Process process = new Process();
        model.addProcess(process);
        process.setId("myProcess");
        process.setName("My Process");

        //2. 开始事件
        StartEvent startEvent = new StartEvent();
        startEvent.setId("startEvent");
        process.addFlowElement(startEvent);

        //3. 用户任务
        UserTask userTask = new UserTask();
        userTask.setName("User Task");
        userTask.setId("userTask");
        process.addFlowElement(userTask);

        //4. 结束事件
        EndEvent endEvent = new EndEvent();
        endEvent.setId("endEvent");
        process.addFlowElement(endEvent);

        //5. 添加流程顺序
        process.addFlowElement(new SequenceFlow("startEvent","userTask"));
        process.addFlowElement(new SequenceFlow("userTask","endEvent"));

        return model;

    }

}
